from google.appengine.ext import db
import Model
import User
import ValidateData

class WorkOutController:
   def __init__(self,u):
      self.user_name = u

   def storeData(self, cal, t):
      self.calorie = float(cal) 
      self.time = t
      checkUser = ValidateData.isUserExist(self.user_name)
      checkHeartRateData = ValidateData.isHeartRateDataExist(self.time)
      if checkUser and not checkHeartRateData:
         error = "NONE"
         user_object = ValidateData.getUserInfo(self.user_name)
         w = Model.HeartRateModel(user = user_object[0].key(),
                              calorie = self.calorie,
                              workoutTime = self.time)
         #Save the data to the datastore
         w.put()
         return error
      else:
         return "Error: user is not existed or heart rate data is existed."

   def getAllData(self):
      # the limit of the size will be 1000
      data = ValidateData.getAllHeartRateData(self.user_name, 1000)
      return data
